//
//  ZWUserAccount.swift
//  张伟的微博
//
//  Created by 伟 on 16/2/29.
//  Copyright © 2016年 伟. All rights reserved.
//

import UIKit

class ZWUserAccount: NSObject,NSCoding {
    
    // 用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    // access_token的生命周期，单位是秒数。
    // 在Swift中如果是基本数据类型定义为可选,在KVC会找不到这个key
    var expires_in: NSTimeInterval = 0.0{
        didSet{
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    } //基本数据类型不要？
    
    
    var expiresDate:NSDate?
    //当前用户的uid
    var uid: String?
    //用户昵称
    var screen_name: String?
    //图像地址 80*80
    var avatar_large: String?

    //字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        //只有super.init后，对象才创建完毕，对象创建完毕后才可以调用对象
        self.setValuesForKeysWithDictionary(dict)
    }
    
    //当字典在key中找不到模型时
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //用归档和解档的时候，需要NScode
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name,forKey: "screen_name")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    
    override var description :String{
    
        let key = ["access_token","expires_in","expiresDate","uid"]
        return "UserAccount的模型\(dictionaryWithValuesForKeys(key).description)"
    }
    
    //

    
}
